package com.googlecode.tidej.shared.jsdoc;

import java.util.ArrayList;
import java.util.List;

public class JsDoc {
  String documentation;
  List<String[]> params = new ArrayList<String[]>();
  String[] returns;
  String[] type;
  boolean constructor;
  boolean constant;
  
  static String[] split(String s, int count) {
    String[] parts = new String[count];
    for (int i = 0; i < count - 1; i++) {
     int cut = s.indexOf(' ');
     if (cut == -1) {
       parts[i] = s;
       s = "";
       continue;
     }
     parts[i] = s.substring(0, cut);
     s = s.substring(cut + 1).trim();
    }
    parts[count - 1] = s;
    return parts;
  }
  
  public JsDoc(String jsdoc) {
    String [] lines = jsdoc.split("\\n");
    StringBuilder rebuilt = new StringBuilder();
    for (String line: lines) {
      if (line.trim().startsWith("*")) {
        int cut = line.indexOf('*');
        line = line.substring(cut + 1).trim();
      }
      rebuilt.append(line);
      rebuilt.append("\n");
    }
    
    jsdoc = rebuilt.toString();
    String[] tags = jsdoc.split("@");
    documentation = tags[0].trim();
    for (int i = 1; i < tags.length; i++) {
      String tag = tags[i];
      if (tag.startsWith("param ")) {
        String[] parts = split(tag, 4);
        params.add(parts);
      } else if (tag.startsWith("constructor")) {
        constructor = true;
      } else if (tag.startsWith("returns")) {
        returns = split(tag, 3);
      } else if (tag.startsWith("type")) {
        type = split(tag, 3);
      } else if (tag.startsWith("const")) {
        constant = true;
      } else {
        documentation += "\n@" + tag;
      }
    }
  }

}
